package com.vcc.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.core.exception.NotFoundException;
import com.vcc.data.entity.Currency;
import com.vcc.data.mapper.CurrencyMapper;
import com.vcc.service.boss.request.currency.CurrencyDto;
import com.vcc.service.service.CurrencyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CurrencyServiceImpl extends ServiceImpl<CurrencyMapper, Currency> implements CurrencyService {

    @Override
    public Boolean existsById(Long id) {
        return queryChain().eq(Currency::getId, id).exists();
    }


    @Override
    public Currency getById(Long id) {
        return queryChain().eq(Currency::getId, id).oneOpt().orElseThrow(() -> new NotFoundException("没有当前币种"));
    }


    @Transactional
    @Override
    public Currency addCurrency(CurrencyDto currencyDto) {
        Currency currency = new Currency();
        currency.setCode(currencyDto.getCurrency());
        currency.setName(currencyDto.getName());
        currency.setStatus(0);
        save(currency);
        return currency;
    }

    @Override
    public Currency getByCode(String code) {
        return queryChain().eq(Currency::getCode, code).oneOpt().orElseThrow(() -> new NotFoundException("没有当前币种"));
    }
}
